using System;
using System.Collections.Generic;
using System.Text;
using GarageGames.Torque.T2D;
using Microsoft.Xna.Framework;
using GarageGames.Torque.Core;
using GarageGames.Torque.Materials;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using GarageGames.Torque.XNA;
using Xyne.Utilities;

namespace Xyne.Templates.Card
{
    class Card : T2DStaticSprite
    {
        public static Vector2 DefaultSize = new Vector2(200, 100);

        public static Card Template = new Card();
        private static SimpleMaterial material;
        private Texture2D texture;
        private List<CardIcon> cardIcons;

        private const string CardBackgroundTextureName = "CardBackgroundTexture";
        private const string CardBackgroundFilename = @".\data\images\ui\card\cardbackground";

        #region Properties
        public static SimpleMaterial DefaultMaterial
        {
            get
            {
                if (material == null)
                {
                    material = new SimpleMaterial();
                    material.Name = CardBackgroundTextureName;
                    material.IsColorBlended = true;
                    material.TextureFilename = @"data\images\ui\card\cardbackground.png";
                }
                return material;
            }
        }

        public static Texture2D StandardBackgroundTexture
        {
            get
            {
                return TorqueEngineComponent.Instance.Game.Content.Load<Texture2D>(CardBackgroundFilename);
            }
        }

        public Texture2D BackgroundTexture
        {
            get { return texture; }
            set { texture = value; }
        }

        [TorqueCloneIgnore]
        public List<CardIcon> Icons
        {
            get
            {
                return cardIcons;
            }

            set
            {
                cardIcons = value;
            }
        }
        #endregion

        #region Constructor/CopyTo
        public Card()
        {
            IsTemplate = true;
            Name = Game.CardName;
            
            Layer = Game.CardLayer;
            Size = Card.DefaultSize;
            ObjectType = TorqueObjectDatabase.Instance.GetObjectType(Name);

            Material = DefaultMaterial;

            BackgroundTexture = TorqueEngineComponent.Instance.Game.Content.Load<Texture2D>(CardBackgroundFilename);
            float cardIconLinkPointDiff = (CardIcon.Template.Size.X / Size.X)+.05f;

            T2DLinkPointComponent linkPointComponent = new T2DLinkPointComponent();
            linkPointComponent.AddLinkPoint("Icon1", new Vector2(-cardIconLinkPointDiff*4f, -.4f), 0.0f);
            
            linkPointComponent.AddLinkPoint("Icon2", new Vector2(-cardIconLinkPointDiff*2.25f, -.4f), 0.0f);

            linkPointComponent.AddLinkPoint("Icon3", new Vector2(-cardIconLinkPointDiff/2, -.4f), 0.0f);
            Components.AddComponent(linkPointComponent);
            #region InitCardIcons
            cardIcons = new List<CardIcon>();
            CardIcon cardIcon1 = (CardIcon)CardIcon.Template.Clone();
            cardIcon1.Material = MaterialHelper.Instance.GetIconMaterial(null);

            cardIcon1.Mount(this, "Icon1", true);
            cardIcons.Add(cardIcon1);

            CardIcon cardIcon2 = (CardIcon)CardIcon.Template.Clone();
            cardIcon2.Material = MaterialHelper.Instance.GetIconMaterial(null);

            cardIcon2.Mount(this, "Icon2", true);

            cardIcons.Add(cardIcon2);

            CardIcon cardIcon3 = (CardIcon)CardIcon.Template.Clone();
            cardIcon3.Material = MaterialHelper.Instance.GetIconMaterial(null);

            cardIcon3.Mount(this, "Icon3", true);

            cardIcons.Add(cardIcon3);
            #endregion
        }

        public override void CopyTo(TorqueObject obj)
        {
            base.CopyTo(obj);

            if (obj is Card)
            {
                Card card = obj as Card;
                card.BackgroundTexture = BackgroundTexture;
              //  card.cardIcon = (CardIcon)cardIcon.Clone();
            }
        }

        public override object Clone()
        {
            Card theClone = ((Card)base.Clone());

            theClone.Material = ((SimpleMaterial)theClone.Material.Clone());
            return theClone;
        }
        #endregion

        public void ResetBackground()
        {
            BackgroundTexture = TorqueEngineComponent.Instance.Game.Content.Load<Texture2D>(CardBackgroundFilename);
        }

        public void SetCrackedBackground(int level)
        {
            switch (level)
            {
                case 1:
                    BackgroundTexture = TorqueEngineComponent.Instance.Game.Content.Load<Texture2D>(@".\data\images\ui\card\cardbackgroundcrack1");
                    break;
                case 2:
                    BackgroundTexture = TorqueEngineComponent.Instance.Game.Content.Load<Texture2D>(@".\data\images\ui\card\cardbackgroundcrack2");
                    break;
                case 3:
                    BackgroundTexture = TorqueEngineComponent.Instance.Game.Content.Load<Texture2D>(@".\data\images\ui\card\cardbackgroundcrack3");
                    break;
                default:
                    BackgroundTexture = TorqueEngineComponent.Instance.Game.Content.Load<Texture2D>(CardBackgroundFilename);
                    break;
            }
        }
    }
}
